Skip to main content

Class: CredentialManager

The Credential Manager interacts with the agent to handle credentials, allowing operations such as fetching stored credentials, verifying their integrity (e.g., ensuring JWT-based credentials are valid), and deleting them when no longer needed. It also supports error handling during verification and storage processes.


Method: getList

The getList() method is used to retrieve a paginated list of verifiable credentials stored within the system. This function provides the ability to filter and sort the results based on various criteria, such as issuer DID, credential type, issuance and expiration dates, and other attributes. It is a powerful method for querying stored credentials and retrieving relevant data in a structured and scalable manner.

  • Purpose: The purpose of the getList() method is to fetch a list of verifiable credentials, with options to filter, sort, and paginate the results. This allows developers to efficiently retrieve specific credentials based on their search criteria.

  • Parameters: The method accepts a CredentialListRequest object, which can contain the following fields:

    • id?: string[] — (Optional) A list of specific credential IDs to retrieve.
    • issuerDid?: string — (Optional) The decentralized identifier (DID) of the issuer of the credentials.
    • type?: string — (Optional) The type of the credentials being searched.
    • format?: CredentialFormat — (Optional) The format of the credentials (e.g., JWT or LD-Proof).
    • issuanceDateFrom?: Date — (Optional) The start date from which the credentials were issued.
    • issuanceDateTo?: Date — (Optional) The end date up to which the credentials were issued.
    • expirationDateFrom?: Date — (Optional) The start date from which the credentials expire.
    • expirationDateTo?: Date — (Optional) The end date up to which the credentials expire.
    • attributes?: CredentialAttributeRequest — (Optional) Attributes to filter the credentials by.
    • sortOrder?: CredentialSortOrderRequest[] — (Optional) The order in which to sort the results (e.g., by issuance date, expiration date).

    Additionally, the method includes pagination properties through PaginationModel:

    • currentPage: number — The page of results to retrieve.
    • rowsPerPage: number — The number of results per page.
  • Returns: Promise<PagedResult<Credential>> — Resolves to a PagedResult object containing a list of verifiable credentials for the specified page and filters. If no credentials are found, it returns an empty list.

Usage Example

  1. Basic Usage - Fetching All Credentials:
// Fetch the first 1000 verifiable credentials
const result = await agent.credentialManager.getList({
currentPage: 1,
rowsPerPage: 1000,
});
console.log("Fetched credentials:", result);
return result.result;
  1. Advanced Usage - Fetching by Issuer DID and Type:

This example demonstrates how to use additional filters, such as issuer DID and credential type.

// Fetch credentials filtered by issuer DID and type, with pagination
const filterCriteria = {
currentPage: 1,
rowsPerPage: 50,
issuerDid: "did:web:example.issuer", // Hardcoded issuer DID
type: "VerifiableCredential", // Hardcoded credential type
issuanceDateFrom: new Date("2023-01-01"), // Hardcoded issuance start date
issuanceDateTo: new Date("2023-12-31"), // Hardcoded issuance end date
};

const result = await agent.credentialManager.getList(filterCriteria);
console.log("Filtered credentials:", result.result);

Example Output (Success)

{
"currentPage": 1,
"rowsPerPage": 100,
"totalRows": 1000,
"result": [
{
"id": "12345",
"type": "VerifiableCredential",
"issuerDid": "did:web:example.issuer",
"issuanceDate": "2023-03-10T12:00:00Z",
"expirationDate": "2024-03-10T12:00:00Z",
"attributes": {
"name": "John Doe",
"email": "john.doe@example.com"
}
}
]
}

Common Use Cases

  • Retrieving All Credentials: Use this method to fetch a large list of credentials in the system, with pagination support for scalability.
  • Filtering Credentials: By using filters such as issuerDid, type, and date ranges, you can narrow down the results to specific credentials.
  • Sorting and Pagination: The method supports sorting the credentials based on attributes like issuance or expiration date and paginating through the results for large datasets.

Method: deleteById

The deleteById() method is used to delete one or more verifiable credentials from the wallet by providing their unique credential IDs. This method attempts to delete each credential, returning the IDs of the successfully deleted credentials and any errors encountered during the process for failed deletions.

  • Purpose: The purpose of the deleteById() method is to remove verifiable credentials from the wallet by their unique ID. It ensures that credentials can be deleted when no longer needed, and it provides feedback on both successful and failed deletion attempts.

  • Parameters:

    • credentialIds: string[] — A list of credential IDs to delete. Each ID represents a unique verifiable credential stored in the wallet.
  • Returns: Promise<OperationDataResult<CredentialDeleteResult>> — Resolves to an OperationDataResult object that contains:

    • deleted: string[] — A list of successfully deleted credential IDs.
    • failed: { id: string, error: string }[] — A list of failed deletions with the respective error messages.

Usage Example

  1. Deleting Multiple Credentials

This example demonstrates how to use deleteById() to delete multiple credentials by their IDs.

const credentialIds = ["credential1-id", "credential2-id"]; // Hardcoded credential IDs

const agent = await initializeAgent();
if (!agent) {
throw new Error("Agent not initialized");
}

// Delete credentials by ID
const result = await agent.credentialManager.deleteById(credentialIds);

if (!result.isSuccessful) {
console.error("Some credentials failed to delete:", result.result.failed);
} else {
console.log(
"All selected credentials were deleted successfully:",
result.result.deleted
);
}

Example Output (Success - All Credentials Deleted)

{
"isSuccessful": true,
"result": {
"deleted": ["credential1-id", "credential2-id"],
"failed": []
}
}

Common Use Cases

  • Removing Unnecessary Credentials: Use this method to clean up verifiable credentials that are no longer needed or valid in your application.
  • Error Handling for Credential Deletion: This method allows you to handle deletion errors effectively, providing detailed feedback on which credentials failed to delete and why.

Method: getUITemplate

The getUITemplate() method is used to retrieve the UI template code associated with a credential. This template can be used to display or interact with the credential's details in a user interface. It fetches the template based on the provided contact ID, schema ID, and optionally a namespace.

  • Purpose: The purpose of this method is to retrieve the UI template that is associated with the details of a specific credential. This allows developers to dynamically render UI components based on the credential's schema and metadata.

  • Parameters:

    • contactId: string — The unique identifier of the contact (connection) for which the UI template is being fetched.
    • schemaId: string — The identifier of the credential schema, used to determine which UI template to retrieve.
    • namespace?: string — (Optional) The namespace related to the credential, which may be used to further narrow down the template.
  • Returns: Promise<OperationDataResult<string | undefined>> — Resolves to an OperationDataResult object containing the UI template code as a string, or undefined if the template could not be found. If unsuccessful, it returns an error message.

Usage Example

This example demonstrates how to use the getUITemplate() method to retrieve a UI template for a credential.

const contactId = "18034a83-8b1e-4c05-ae75-5fc9d183d8c7"; // Hardcoded contact ID
const schemaId = "schema-12345"; // Hardcoded schema ID
const namespace = "credentialNamespace"; // Optional hardcoded namespace

// Fetch the UI template for the credential
const result = await agent.credentialManager.getUITemplate(
contactId,
schemaId,
namespace
);

if (!result.isSuccessful) {
console.error(
`Failed to fetch UI template. Status: ${result.errorCode}. Error: ${result.error}`
);
} else {
console.log("UI Template Code:", result.result);
}

Example Output (Success)

{
"isSuccessful": true,
"result": "<div><h1>Credential Template</h1><p>Details of the credential...</p></div>"
}

Common Use Cases

  • Dynamically Rendering Credential UI: This method is useful when you need to fetch and display a UI template based on a credential's schema and metadata.
  • Customizing UI for Different Credentials: By retrieving the UI template for specific credentials, you can dynamically adjust the display and interaction logic depending on the type and schema of the credential.

Method: getUITemplateById

The getUITemplateById() method is used to retrieve the UI template associated with a specific credential by its credential ID. This template can be used to dynamically display or interact with the credential's details in a user interface. The method fetches the template based on the credential's schema and issuer details.

  • Purpose: The purpose of this method is to retrieve the UI template associated with a specific verifiable credential, allowing developers to render a custom UI for displaying or interacting with the credential details.

  • Parameters:

    • credentialId: string — The unique identifier of the credential for which the UI template is being fetched.
  • Returns: Promise<OperationDataResult<string | undefined>> — Resolves to an OperationDataResult object containing the UI template code as a string, or undefined if the template could not be found. If unsuccessful, it returns an error message.

Usage Example

This example demonstrates how to use the getUITemplateById() method to retrieve the UI template for a specific credential.

const credentialId = "12345"; // Hardcoded credential ID

// Fetch the UI template for the credential by its ID
const result = await agent.credentialManager.getUITemplateById(credentialId);

if (!result.isSuccessful) {
console.error(
`Failed to fetch UI template. Status: ${result.errorCode}. Error: ${result.error}`
);
} else {
console.log("UI Template Code:", result.result);
}

Example Output (Success)

{
"isSuccessful": true,
"result": "<div><h1>Credential Template</h1><p>Details of the credential...</p></div>"
}

Common Use Cases

  • Rendering Credential UI: This method is useful when you need to dynamically fetch and display a UI template for a credential based on its schema and metadata.
  • Customizing UI for Specific Credentials: By retrieving the UI template for a credential, you can dynamically adjust the display and interaction logic based on the credential's schema and issuer details.

Method: get

The get() method retrieves the full details of a verifiable credential using its unique identifier (ID). This method is useful for accessing all the information related to a specific credential stored in the system.

  • Purpose: The purpose of this method is to fetch and return the complete details of a verifiable credential based on its unique ID. It ensures that developers can retrieve specific credentials and their associated metadata.

  • Parameters:

    • id: string — The unique identifier (ID) of the verifiable credential you want to retrieve.
  • Returns: Promise<OperationDataResult<Credential | undefined>> — Resolves to an OperationDataResult object that contains the full details of the credential as a Credential object. If the credential is not found or an error occurs, it returns an error message.

Usage Example

This example demonstrates how to use the get() method to retrieve the details of a credential by its ID.

const credentialId = "12345"; // Hardcoded credential ID

// Fetch the credential details by its ID
const result = await agent.credentialManager.get(credentialId);

if (!result.isSuccessful) {
console.error(
`Failed to fetch credential details. Status: ${result.errorCode}. Error: ${result.error}`
);
} else {
console.log("Credential Details:", result.result);
}

Example Output (Success)

{
"isSuccessful": true,
"result": {
"id": "12345",
"issuerDid": "did:web:example.issuer",
"type": "VerifiableCredential",
"issuanceDate": "2023-03-10T12:00:00Z",
"expirationDate": "2024-03-10T12:00:00Z",
"attributes": {
"name": "John Doe",
"email": "john.doe@example.com"
}
}
}

Common Use Cases

  • Retrieving Credential Details: Use this method to fetch the full details of a verifiable credential for display or further processing.
  • Error Handling for Credential Fetching: This method provides detailed error messages if the credential is not found or if there is an issue retrieving the details.
X

Graph View